/*
编写 : Hexh
时间 : 2012-5-24 15:41:23
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using HMIS.Infrastructure.Core.Entities;
using System.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of InfraApprove
	/// </summary>
	public class InfraApproveController : BaseController
	{

		private readonly IInfraApproveFacade InfraApproveFacade;
		private readonly IInfraApproveHisFacade InfraApproveHisFacade;
		private readonly IHospUserFacade HospUserFacade;

		public InfraApproveController(IInfraApproveFacade InfraApproveFacade,
			IInfraApproveHisFacade InfraApproveHisFacade,
			IHospUserFacade HospUserFacade)
		{
			this.InfraApproveFacade = InfraApproveFacade;
			this.InfraApproveHisFacade = InfraApproveHisFacade;
			this.HospUserFacade = HospUserFacade;
		}

		[Description("访问InfraApprove管理页面")]
		[ViewPage]
		public ActionResult InfraApprove()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改InfraApprove信息")]
		public ActionResult SaveOrUpdate()
		{
			InfraApprove entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new InfraApprove
				{
					Name = Request["Name"],
					Code = Request["Code"],
					ApproveType = Request["ApproveType"],
					DepartmentId = Request["DepartmentId"],
					State = Request["State"],
					Description = Request["Description"],
				};
			}
			else
			{
				// get the entity.
				entity = InfraApproveFacade.Get(Request["Id"]);
				// set new value..
				entity.Name = Request["Name"];
				entity.Code = Request["Code"];
				entity.ApproveType = Request["ApproveType"];
				entity.DepartmentId = Request["DepartmentId"];
				entity.State = Request["State"];
				entity.Description = Request["Description"];
			}
			// 保存信息
			InfraApproveFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除InfraApprove基本资料")]
		public ActionResult Delete()
		{
			InfraApproveFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("物理删除InfraApprove基本资料")]
		public ActionResult PhysicsDelete()
		{
			InfraApproveFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取InfraApprove清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = InfraApproveFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			// 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<InfraApprove> { total = total, data = data },
				new string[] { "Department" });
		}

		[Description("获取InfraApprove信息")]
		public ActionResult GetById()
		{
			var entity = InfraApproveFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<InfraApprove> { data = new InfraApprove[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断InfraApprove字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = InfraApproveFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}

		[Description("获取审批流程记录清单")]
		public ActionResult GetApproveHisPlist()
		{
			long total;
			var data = InfraApproveHisFacade.GetSteplist(
				Request["apprhisid"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);

			// 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<InfraApproveHisStep> { total = total, data = data },
				new string[] { "" });
		}

		[ExtResult]
		[Description("发送审批短信")]
		public ActionResult SendApproveCodeSms()
		{
			string[] Ids = Request["Ids"].Split(',');
			for (int i = 0; i < Ids.Length; i++)
			{
				if (!string.IsNullOrEmpty(Ids[i]))
				{
					InfraApproveFacade.SendApproveCodeSms(Ids[i]);
				}
			}
			return this.JsonFormat(new ExtResult { success = true });
		}
	}
}
